﻿using Doc.SDK.Core.Model;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Doc.SDK.Core
{
    public class HttpRequest
    {
        public static async Task<T> GetAsync<T>(string url)
        {
            var httpClient = new HttpClient();
            
            string response = await httpClient.GetStringAsync(url);

            T t = JsonConvert.DeserializeObject<T>(response);

            return t;
        }

        public static async Task<T> GetAsync<T>(string url,QQDocHeaders header)
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("Access-Token", header.AccessToken);
            httpClient.DefaultRequestHeaders.Add("Client-Id", header.ClientId);
            httpClient.DefaultRequestHeaders.Add("Open-Id", header.OpenId);
            string response = await httpClient.GetStringAsync(url);

            T t = JsonConvert.DeserializeObject<T>(response);

            return t;
        }

        public static async Task<T> PostAsync<T>(string url, QQDocHeaders header, string body = null)
        {
            StringContent httpContent = null;
            if (!string.IsNullOrEmpty(body))
            {
                httpContent = new StringContent(body, Encoding.UTF8, "application/json");
            }

            var httpClient = new HttpClient();
            //httpClient.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");
            httpClient.DefaultRequestHeaders.Add("Access-Token", header.AccessToken);
            httpClient.DefaultRequestHeaders.Add("Client-Id", header.ClientId);
            httpClient.DefaultRequestHeaders.Add("Open-Id", header.OpenId);
            var response = await httpClient.PostAsync(url, httpContent);

            string result = await response.Content.ReadAsStringAsync();

            T t = JsonConvert.DeserializeObject<T>(result);

            return t;
        }

        public static async Task<T> PatchAsync<T>(string url, QQDocHeaders header, string body = null)
        {
            StringContent httpContent = null;
            if (!string.IsNullOrEmpty(body))
            {
                httpContent = new StringContent(body, Encoding.UTF8, "application/json");
            }

            var httpClient = new HttpClient();
            //httpClient.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");
            httpClient.DefaultRequestHeaders.Add("Access-Token", header.AccessToken);
            httpClient.DefaultRequestHeaders.Add("Client-Id", header.ClientId);
            httpClient.DefaultRequestHeaders.Add("Open-Id", header.OpenId);
            var response = await httpClient.PatchAsync(url, httpContent);

            string result = await response.Content.ReadAsStringAsync();

            T t = JsonConvert.DeserializeObject<T>(result);

            return t;
        }

        public static async Task<T> DeleteAsync<T>(string url, QQDocHeaders header)
        {
          
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("Access-Token", header.AccessToken);
            httpClient.DefaultRequestHeaders.Add("Client-Id", header.ClientId);
            httpClient.DefaultRequestHeaders.Add("Open-Id", header.OpenId);
            var response = await httpClient.DeleteAsync(url);

            string result = await response.Content.ReadAsStringAsync();

            T t = JsonConvert.DeserializeObject<T>(result);

            return t;
        }
    }
}
